package com.hspedu.smallchange;

import com.sun.javafx.iio.gif.GIFImageLoaderFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.SimpleTimeZone;

public class SmallChangeSys {
    public static void main(String[] args) {
        //定义相关变量
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        String key = "";

        //1.完成零钱通明细
        //使用 String 拼接
        String details = "--------------零钱通明细--------------";

        //2.完成收益入账
        double money = 0;
        double balance = 0;
        Date date = null;// date 是 java.util.Date 类型，表示日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//可以用于日期格式化

        //3.完成消费
        String name = "";

        do {
            System.out.println("\n==============零钱通菜单==============");
            System.out.println("\t\t\t1.零钱通明细");
            System.out.println("\t\t\t2.收益入账");
            System.out.println("\t\t\t3.消     费");
            System.out.println("\t\t\t4.退     出");
            System.out.print("请选择(1-4):");
            key = scanner.next();

            //使用 switch 分支控制
            switch (key) {
                case "1" :
                    System.out.println(details);
                    break;
                case "2" :
                    System.out.print("收益入账金额：");
                    money = scanner.nextDouble();

                    //校验 money 范围
                    if (money <= 0) {
                        System.out.println("输入金额有误，请重新输入");
                        break;
                    }

                    balance += money;
                    //拼接收益入账到details
                    date = new Date();//获取当前日期
                    details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t余额：" + balance;

                    break;
                case "3" :
                    System.out.print("消费店名：");
                    name = scanner.next();
                    System.out.print("消费金额：");
                    money = scanner.nextDouble();

                    //校验 money 范围
                    if (money > balance || money <= 0) {
                        System.out.println("您的消费金额应该在(0," + balance + "]");
                        break;
                    }

                    balance -= money;
                    //拼接消费到details
                    date = new Date();//获取当前日期
                    details += "\n" + name + "\t-" + money + "\t" + sdf.format(date) + "\t余额：" + balance;

                    break;
                case "4" :
                    //再次确定是否退出

                    //方法一
//                    char key1 = ' ';
//                    while (true) {
//                        System.out.print("你确定要退出吗?y/n:");
//                        key1 = scanner.next().charAt(0);
//                        if (key1 == 'y') {
//                            System.out.println("退出");
//                            loop = false;
//                            break;
//                        } else if(key1 != 'n') {
//                            System.out.println("输入不规范，请重新输入");
//                        } else {
//                            break;
//                        }
//                    }

                    //方法二(建议)
                    String choice = "";
                    while (true) {
                        System.out.print("你确定要退出吗?y/n:");
                        choice = scanner.next();
                        if ("y".equals(choice) || "n".equals(choice)) {
                            break;
                        }
                    }

                    //当用户输入正确时，退出 while，进行判断
                    if (choice.equals("y")) {
                        loop = false;
                    }

                    break;
                default:
                    System.out.println("选择有误请重新选择");
            }

        } while (loop);

        System.out.println("==============退出了零钱通==============");
    }
}
